/*
 * Copyright 2008 Jiri Janak
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.floorballcoaches.exercisedrawer;

import com.floorballcoaches.exercisedrawer.objects.Exercise;
import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;

/**
 *
 * @author Jiri Janak
 */
public class ExerciseDrawer {

    public Icon getIcon(String key) {
        return null;
    }

    public Image getImage(Exercise exercise) {
        return null;
    }

    public Image getImage(File exerciseXMLFile) {
        PersistanceTools pt = new PersistanceTools();
        Exercise ex = pt.loadXML(exerciseXMLFile);
        return this.getImage(ex);
    }

    public Image getImage(String exerciseXML) {
        PersistanceTools pt = new PersistanceTools();
        Exercise ex = pt.loadXML(exerciseXML);
        return this.getImage(ex);
    }

    public void saveImageAsPNG(Exercise exercise, File pngFile) {
        Image img = this.getImage(exercise);
        this.saveImageAsPNG(img, pngFile);
    }

    public void saveImageAsPNG(File exerciseXMLFile) {
        String pngFileName = exerciseXMLFile.getAbsolutePath()+".png";
        this.saveImageAsPNG(exerciseXMLFile, pngFileName);
    }

    public void saveImageAsPNG(File exerciseXMLFile, File pngFile) {
        Image img = this.getImage(exerciseXMLFile);
        this.saveImageAsPNG(img, pngFile);
    }

    public void saveImageAsPNG(File exerciseXMLFile, String outputFilePath) {
        Image img = this.getImage(exerciseXMLFile);
        File pngFile = new File(outputFilePath);
        this.saveImageAsPNG(img, pngFile);
    }

    public void saveImageAsPNG(Image img, File pngFile) {
        try {
            RenderedImage ri = (RenderedImage) img;
            ImageIO.write(ri, "png", pngFile);
        } catch (IOException ex) {
            Logger.getLogger(ExerciseDrawer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void saveImageAsPNG(String exerciseXML, File pngFile) {
        Image img = this.getImage(exerciseXML);
        this.saveImageAsPNG(img, pngFile);
    }

}
